# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class Solution:
    def removeElements(self, head: ListNode, val: int) -> ListNode:
        temp: ListNode = head
        it: ListNode = head
        while it is not None:
            if it.val == val:
                temp.next = it.next
                it = temp

            temp = it
            it = it.next

        if head is not None and head.val == val:
            return head.next
        return head
